package cn.edu.sjtu.se.dslab.rhapsody.parse;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import cn.edu.sjtu.se.dslab.component.data.AvionicsEquipment;
import cn.edu.sjtu.se.dslab.component.design.DesignPatternComponent;
import cn.edu.sjtu.se.dslab.component.design.DesignPatternType;
import cn.edu.sjtu.se.dslab.helper.ApplicationContextHelpler;
import cn.edu.sjtu.se.dslab.project.DisplayZone;
import cn.edu.sjtu.se.dslab.project.EntityZone;
import cn.edu.sjtu.se.dslab.service.IAvionicsEquipmentService;
import cn.edu.sjtu.se.dslab.service.IDesignPatternComponentService;
import cn.edu.sjtu.se.dslab.service.IDisplayZoneService;
import cn.edu.sjtu.se.dslab.service.IEntityZoneService;
import cn.edu.sjtu.se.dslab.service.IProjectService;

public class XmiConverter {

	private static final Logger logger = Logger.getLogger(XmiConverter.class);

	private XmiDocument doc;

	private IDesignPatternComponentService designPatternComponentService;
	private IAvionicsEquipmentService avionicsEquipmentService;
	private IEntityZoneService entityZoneService;
	private IDisplayZoneService displayZoneService;
	private IProjectService projectService;

	public XmiConverter(XmiDocument doc) {
		this.doc = doc;
		designPatternComponentService = (IDesignPatternComponentService)ApplicationContextHelpler.getBean("designPatternComponentService");
		avionicsEquipmentService = (IAvionicsEquipmentService)ApplicationContextHelpler.getBean("avionicsEquipmentService");
		entityZoneService = (IEntityZoneService)ApplicationContextHelpler.getBean("entityZoneService");
		displayZoneService = (IDisplayZoneService)ApplicationContextHelpler.getBean("displayZoneService");
		projectService = (IProjectService)ApplicationContextHelpler.getBean("projectService");
	}
	
	public void mapToProject(long projectId){
		//toBusDesignPatternComponent();
		toEntityZones(projectId);
		toDisplayZones(projectId);
	}
	
	private void toEntityZones(long projectId) {
		for (EntityZone ez: doc.getEntityZones()){
			logger.info("Add new EntityZone name=" + ez.getName() + " size=" + ez.getSize() + " entityUri=" + ez.getEntityUri());
			entityZoneService.addEntityZone(ez, projectId);
		}
	}
	
	private void toDisplayZones(long projectId) {	
		for (DisplayZone dz: doc.getDisplayZones()){
			logger.info("Add new DisplayZone name=" + dz.getName() + " size=" + dz.getSize() + " entityUri=" + dz.getEntityUri());
			displayZoneService.addDisplayZone(dz, projectId);
		}
	}


	private void toBusDesignPatternComponent() {
		DesignPatternComponent dpc = designPatternComponentService
				.getDesignPatternCommponentByType(3, DesignPatternType.BUS);

		for (XmiType xt : doc.getUmlPackage("MethodologyDemo", "ActorPkg")
				.getChildren()) {
			ProfileType pt = doc.getProfileType(
					XmiConst.L_ARCHITECTURE_OUTERSYSTEM
							.getQualifiedName(), XmiConst.A_BASE_CLASS, xt
							.getXmiId());
			if (pt != null) {
				AvionicsEquipment equipment = avionicsEquipmentService
						.getAvionicsEquipmentByName(xt.getName());
				if (equipment == null) continue;
				logger.info("Equipment:" + equipment.getName() + "--"
						+ equipment.getUri());
				AvionicsEquipment bus = avionicsEquipmentService
						.getAvionicsEquipmentByName(pt.getProperties().get(
								XmiConst.A_BUS_TYPE));
				logger.info(pt.getProperties().get(XmiConst.A_BUS_TYPE));
				if (bus == null) continue;
				logger.info("Bus:" + bus.getName() + "--" + bus.getUri());
				Map<String, String> propertyMap = new HashMap<String, String>();
				propertyMap.put("equipment", equipment.getUri());
				propertyMap.put("bus_type", bus.getUri());
				propertyMap.put("bus_data_area", "ahrs_data_area");
				propertyMap.put("entity_data_area", "/");
				propertyMap.put("equipment_number", "23");
				propertyMap.put("read_bus_processing_method", "40");
				propertyMap.put("read_frequency", "55");
				propertyMap.put("write_bus_processing_method", "41");
				propertyMap.put("write_frequency", "66");
				designPatternComponentService.addDpcItem(propertyMap,
						dpc.getUri());
			}
		}
	}
}
